import * as BABYLON from "babylonjs"; //全部引入
import getPath from "./getPath"; // 获取动画运动路径
import getAnmKeys from "./getAnmKeys"; // 获取动画帧
import getCurve from "./getCurve"; // 获取曲线路径

// 添加box充当小车
function addSimpleCar(scene) {
  let smallbox = addSimpleBox(scene);

  // 克隆车 克隆两个box
  let sbox2 = smallbox.clone("sbox2");
  let sbox3 = smallbox.clone("sbox3");
  // 克隆一个走曲线的车
  let sbox4 = smallbox.clone("sbox4");
  let sbox5 = smallbox.clone("sbox5");
  let sbox6 = smallbox.clone("sbox6");
  let sbox7 = smallbox.clone("sbox7");
  // sbox4.visibility = 0;
  // sbox5.visibility = 0;
  // sbox6.visibility = 0;
  // sbox7.visibility = 0;

  // 拖尾物体
  let trail = new BABYLON.TrailMesh("new", sbox4, scene, 0.04, 8, true);
  let trail2 = new BABYLON.TrailMesh("new", sbox5, scene, 0.04, 8, true);
  let trail3 = new BABYLON.TrailMesh("new", sbox6, scene, 0.04, 8, true);
  let trail4 = new BABYLON.TrailMesh("new", sbox7, scene, 0.04, 8, true);
  const trailMat = new BABYLON.StandardMaterial("trailMat");
  trailMat.emissiveColor = trailMat.diffuseColor = new BABYLON.Color3.Yellow();
  trailMat.specularColor = new BABYLON.Color3.Black();
  trail.material = trailMat;
  trail2.material = trailMat;
  trail3.material = trailMat;
  trail4.material = trailMat;

  // 汽车2路径
  let xyPath2 = [
    [-2.3, 5],
    [-2.4, -4]
  ];
  // 汽车3路径
  let xyPath3 = [
    [-2.6, -6],
    [-2.5, 7]
  ];
  // 添加行驶路径动画
  // 汽车路径
  let xyPath = [
    [-3.3, 10],
    [6, -10]
  ];
  // 行驶路线三维坐标点连线
  let linePath1 = getPath(xyPath, 0);
  let linePath2 = getPath(xyPath2, 0);
  let linePath3 = getPath(xyPath3, 0);
  let linePath4 = getCurve([0, 0], [-5, 0]);
  let linePath5 = getCurve([0, 0], [-6, 3]);
  let linePath6 = getCurve([0, 0], [6, -5]);
  let linePath7 = getCurve([0, 0], [-3, 5]);
  // 小车1动画
  addCardriveLine(scene, smallbox, linePath1);
  // 小车2动画
  addCardriveLine(scene, sbox2, linePath2);
  // 小车3动画
  addCardriveLine(scene, sbox3, linePath3);

  // 小车4 走曲线的小车动画
  addCardriveLine(scene, sbox4, linePath4, trail);
  addCardriveLine(scene, sbox5, linePath5, trail2);
  addCardriveLine(scene, sbox6, linePath6, trail3);
  addCardriveLine(scene, sbox7, linePath7, trail4);
}
// 添加简单正方体
function addSimpleBox(scene) {
  const smallbox = BABYLON.MeshBuilder.CreateBox(
    "smallbox",
    { size: 0.04 },
    scene
  );
  smallbox.position = new BABYLON.Vector3(15, 0, 15);
  smallbox.rotation.y = Math.PI / 3;
  // smallbox.visibility = 0.4;
  let boxMat = new BABYLON.StandardMaterial("boxMat", scene);
  boxMat.emissiveColor = new BABYLON.Color3(1, 0, 0);
  smallbox.material = boxMat;
  return smallbox;
}

// 播放汽车动画
function addCardriveLine(scene, smallbox, linePath, trail) {
  // 车上加灯
  // let light = new BABYLON.PointLight(
  //   "pointLight",
  //   new BABYLON.Vector3(0, 0, 0),
  //   scene
  // );
  // light.parent = smallbox;
  // light.position.y = 0.1;
  // light.diffuse = new BABYLON.Color3(1, 0, 0);
  // light.range = 0.1;
  // light.intensity = 5;
  // 行驶动画效果
  let animationDrive = addCarAnim(linePath);
  let lastKey = 100 - 100 / linePath.length;
  if (trail) {
    // 事件
    let event1 = new BABYLON.AnimationEvent(
      lastKey - 2,
      () => {
        // console.log("sfsf");
        trail.visibility = 0;
      },
      false
    );
    let event2 = new BABYLON.AnimationEvent(
      12,
      () => {
        trail.visibility = 1;
      },
      false
    );
    // 把事件添加到动画中
    animationDrive.addEvent(event1);
    animationDrive.addEvent(event2);
  }

  smallbox.animations = [];
  smallbox.animations.push(animationDrive);
  // 参数5动画播放速度
  scene.beginAnimation(smallbox, 0, lastKey, true, 1);
}
// 添加汽车动画
function addCarAnim(linePath) {
  // 动画帧数组
  let keys = getAnmKeys(linePath);
  // 汽车移动动画
  var animationDrive = new BABYLON.Animation(
    "animationDrive",
    "position",
    30,
    BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
    BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE
  );
  animationDrive.setKeys(keys);
  return animationDrive;
}

export default addSimpleCar;
